package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.dao.notify.GroupTable;

public class GroupAnnouncementProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        BusinessProtocolMessages.GroupAnnouncementProtocol response = (BusinessProtocolMessages.GroupAnnouncementProtocol) msg;
        GroupTable groupTable = null;
        if (response.hasRegisteredGroup()) {
            groupTable = getRegisteredGroup(response.getRegisteredGroup());
            if (groupTable==null) {
                return;
            }
            update(groupTable);
        } else if (response.hasTouristsGroup()) {
            groupTable = getTouristsGroup(response.getTouristsGroup());
            if (groupTable==null) {
                return;
            }
            update(groupTable);
        }
    }

    private GroupTable getRegisteredGroup(BusinessProtocolEntities.RegisteredGroup group) {

        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());
        groupTable.setGroupName(group.getGroupName());
        groupTable.setGroupId(group.getGroupId());
        groupTable.setGroupType(IMSContacts.UserType.REGISTERED_USER);
        if (group.hasGroupAnnouncements()) {
            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
            groupTable.setAnnouncementId(groupS.getAnnouncementId());
        }
        if (group.hasSettings()) {
            BusinessProtocolEntities.Settings set = group.getSettings();
            groupTable.setSnapchatStatus(set.getSnapchatStatus());
            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return groupTable;
    }

    private GroupTable getTouristsGroup(BusinessProtocolEntities.TouristsGroup group) {
        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());
        groupTable.setGroupName(group.getGroupName());
        groupTable.setGroupId(group.getGroupId());
        groupTable.setGroupType(IMSContacts.UserType.TOURISTS_USER);
        if (group.hasGroupAnnouncements()) {
            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
            groupTable.setAnnouncementId(groupS.getAnnouncementId());
        }
        if (group.hasSettings()) {
            BusinessProtocolEntities.Settings set = group.getSettings();
            groupTable.setSnapchatStatus(set.getSnapchatStatus());
            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return groupTable;
    }

    private void getGroupMemberTable(GroupMemberTable groupMemberTable, String groupMemberId, String groupMemberName, String groupMemberNick, String groupMemberUserId, String groupMemberAlias, String onlineStatus, String userType, String groupMemberAvatar, int groupMemberType, String groupId, String groupName, String groupType) {
        groupMemberTable.setGroupMemberId(groupMemberId);
        groupMemberTable.setGroupMemberName(groupMemberName);
        groupMemberTable.setGroupMemberNick(groupMemberNick);
        groupMemberTable.setGroupMemberUserId(groupMemberUserId);
        groupMemberTable.setGroupMemberAlias(groupMemberAlias);
        groupMemberTable.setOnlineStatus(onlineStatus);
        groupMemberTable.setUserType(IMSUtils.userTypeAnalysis(groupMemberUserId));
        groupMemberTable.setGroupMemberAvatar(groupMemberAvatar);
        groupMemberTable.setGroupMemberType(groupMemberType);
        groupMemberTable.setGroupId(groupId);
        groupMemberTable.setGroupName(groupName);
        groupMemberTable.setGroupType(groupType);
    }

    @Override
    public void update(Object... table) {
        super.update(table);
        try {
            GroupTable groupTable = (GroupTable) table[0];
//            GroupMemberTable groupMemberTable = (GroupMemberTable) table[1];
            GroupTable.insert(groupTable);
//            GroupMemberTable.insert(groupMemberTable);

//            ChatContentTable cct = new ChatContentTable();
//            cct.setChatType(Canstants.GROUP_CHAT);
//            cct.setTargetId(groupTable.getGroupId());
//            cct.setTargetName(groupTable.getGroupName());
//            cct.setTargetType(groupTable.getGroupType());
//            List<String> urls = GroupMemberTable.getMemberAvatarUrls(groupTable.getGroupId());
//            cct.setTargetAvatarUrl(TextUtils.join(",", urls));
//            ChatContentTable.insert(cct);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
